<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchForm" class="mb-20">
      <el-form-item label="关键词">
        <el-input v-model="searchForm.keyword" placeholder="输入关键词搜索" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 作品表格 -->
    <el-table :data="opusList" border style="width: 100%">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="opusTitle" label="作品标题" show-overflow-tooltip align="center" />
      <el-table-column prop="opusCover" label="封面" width="180" align="center">
        <template #default="scope">
          <el-image
            v-if="scope.row.opusCover"
            :src="scope.row.opusCover"
            style="width: 80px; height: 80px"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160" align="center">
        <template #default="scope">
          {{ formatTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="作者" width="120" align="center">
        <template #default="scope">
          {{ UserNames[scope.$index] }}
        </template>
      </el-table-column>
      <el-table-column label="分类" width="120" align="center">
        <template #default="scope">
          {{ TopicNames[scope.$index] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" plain size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="success" plain size="mini" @click="handlePreview(scope.row)">预览</el-button>
          <el-button type="danger" plain size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="mt-20"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      :current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getList, deleteOpus } from '@/api/opus' // 后期替换为真实接口
import moment from 'moment'

export default {
  name: 'OpusManage',
  data() {
    return {
      opusList: [], // 作品列表
      UserNames: [], // 作者列表
      TopicNames: [], // 分类列表
      total: 0, // 总记录数
      pageSize: 10, // 每页数量
      currentPage: 1, // 当前页码
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取作品列表
    async fetchData() {
      try {
        const res = await getList({
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm
        })
        this.opusList = res.opusPage.records
        this.total = res.opusPage.total
        this.UserNames = res.UserNames
        this.TopicNames = res.TopicNames
      } catch (error) {
        this.$message.error('获取作品列表失败')
      }
    },
    // 时间格式化
    formatTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        opusTitle: '',
        author: ''
      }
      this.currentPage = 1
      this.fetchData()
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 删除作品
    async handleDelete(row) {
      try {
        await this.$confirm('确定删除该作品?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteOpus({ opusId: row.opusId })
        this.$message.success('删除成功')
        this.fetchData() // 刷新列表
      } catch (error) {
        this.$message.error('操作失败')
      }
    },
    // 占位方法（后续补充）
    handleEdit(row) {
      this.$router.push({
        name: 'OpusEditorPage',
        params: { id: row.opusId }
      })
    },
    handlePreview(row) {
      this.$router.push({
        name: 'OpusDetailPage',
        params: { id: row.opusId }
      })
    }
  }
}
</script>

<style scoped>
.mb-20 {
  margin-bottom: 20px;
}

.mt-20 {
  margin-top: 20px;
}
</style>
